import door from "./door";

/**
*
* @ author:cmd
* @ wechat:codercmd@qq.com
* @ data: 2020-03-04 16:14
*/
export default class game_main extends Laya.Script {
    constructor() {super();}

    game_start_flag:boolean = false;
    player_move_flag:boolean = false;

    start_move_pos:Laya.Vector2 = new Laya.Vector2(0,0);
    cur_move_pos:Laya.Vector2 = new Laya.Vector2(0,0);
    player_speed:number = 2;
    speed_count:number = 0;

    player_node:Laya.Node = null;
    player_node_sprite:Laya.Sprite3D = null;
    player_rig3d:Laya.Rigidbody3D = null;

    onAwake(){
        console.log("game_main");
        
        Laya.Scene3D.load("3dres/LayaScene_SampleScene/Conventional/SampleScene.ls",Laya.Handler.create(this,this.scene_loaded));
    }
    
    scene_loaded(scene:Laya.Scene3D){
        Laya.stage.addChild(scene);
        scene.zOrder = -1;
        orc.event_manager.add_listener(orc.event_id.orc_begin_event,this.game_start.bind(this));

        this.player_node = scene.getChildByName('player');
        this.player_node_sprite = this.player_node as Laya.Sprite3D;
        this.player_rig3d = this.player_node.getComponent(Laya.Rigidbody3D);
        
        scene.getChildByName('door').addComponent(door);
    }

    game_start(){
        this.game_start_flag = true;
        
    }

    onMouseDown(){
        console.log("mouse down",Laya.stage.mouseX,Laya.stage.mouseY);
        if(this.game_start_flag)
        {
            this.player_move_flag = true;
            this.speed_count = 0;
        }
        this.start_move_pos = new Laya.Vector2(Laya.stage.mouseX,Laya.stage.mouseY);
        this.cur_move_pos = new Laya.Vector2(Laya.stage.mouseX,Laya.stage.mouseY);
        this.player_rig3d.linearDamping = 0.01;
    }

    onMouseUp(){
        console.log("mouse up",Laya.stage.mouseX,Laya.stage.mouseY);
        this.player_move_flag = false;
        // this.player_rig3d.linearDamping = 5;
    }

    onMouseMove(){
        
        this.cur_move_pos = new Laya.Vector2(Laya.stage.mouseX,Laya.stage.mouseY);
    }

    onMouseOut(){
        console.log("mouse out",Laya.stage.mouseX,Laya.stage.mouseY);
        // var rig3d:Laya.Rigidbody3D = this.player_node.getComponent(Laya.Rigidbody3D);
        // rig3d.linearVelocity = new Laya.Vector3(0,0,0);
        this.player_move_flag = false;
    }

    onUpdate(){
        if(this.player_move_flag)
        {
            this.speed_count = this.speed_count + Laya.timer.delta/1000;
            if(this.speed_count > 0.2)
            {
                this.speed_count =0;               
            }

            var move_subtract:Laya.Vector2 =  new Laya.Vector2(this.cur_move_pos.x - this.start_move_pos.x, this.cur_move_pos.y - this.start_move_pos.y);
            var move_normalize:Laya.Vector2 = new Laya.Vector2(0,0);
            Laya.Vector2.normalize(move_subtract,move_normalize);
            //console.log(move_subtract,move_normalize);
            
            this.player_rig3d.linearVelocity = new Laya.Vector3(-move_normalize.y*this.player_speed,0,move_normalize.x*this.player_speed);
            let r3 = Math.atan2(move_normalize.x,move_normalize.y) *180 / Math.PI;
            this.player_node_sprite.transform.localRotationEulerY = r3;
        }
    }

    // onMouseMove(){
    //     console.log("mouse move");
    // }
}